package persistence;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import domain.Day;
import domain.Schedule;

public class DayHandler {
	
	Connection conn;
	
	public DayHandler() throws SQLException{
		try {
			conn = DBConnection.getInstance().getDb_conn();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	public boolean create(Day day) throws SQLException{
		
		String sqlComand = "INSERT INTO Day VALUES (";
		
		try {
			Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
			sqlComand += "name = " + day.getName() + "range = " + day.getRange() + ");";
			stmt.execute(sqlComand);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return false;		
	}
	
	public boolean delete(Day day){
		
		String sqlComand = "DELETE FROM Day WHERE ";
		
		try {
			Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
			sqlComand += "name = " + day.getName() + ";";
			stmt.execute(sqlComand);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
	
		return false;		
	}
	
	public boolean update(Day day){
		
		String sqlComand = "UPDATE Day SET ";
		
		try {
			Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
			sqlComand += "name = " + day.getName() + ";";
			stmt.execute(sqlComand);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return false;		
	}
	
	public ResultSet retreive(Day day){		
		
		ResultSet result;

		String sqlComand = "SELECT * FROM Day WHERE ";
		
		try {
			Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
			sqlComand += "name = " + day.getName() + ";";
			result = stmt.executeQuery(sqlComand);
			return result;
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		return null;		
	}


}
